use dta/ImportVotesParty.dta, clear

/* CREATING DISTRICT LEVEL VARIABLES */

egen SMD_district=max(valgkrets), by(knr)   /* valgkrets is between 101 and 2002 pre-reform and between 1 and 31 post-reform */
egen PR_district=min(valgkrets), by(knr)    /* valgkrets is between 101 and 2002 pre-reform and between 1 and 31 post-reform */

** oslo (5), drammen (2), kristiansand (2) stavanger (2), bergen (4), trondheim (4)
** ... consist of multiple districts within a municipality. We do not have the possibility to get post-reform data on that level
** ... so we lose these 19 districts. Leaving us with 126-19-12=95 districts.
** some municipalities are split after reform , need to manually find their SMD_district 

sort knr year
li knr SMD_district if SMD_district<100

replace SMD_district=101 if knr==126 /* 126 mysen was part of 125 eidsberg up to 1920 */
replace SMD_district=203 if knr==213 /* 213 ski was part of 212 kråkstad up to 1931 */
replace SMD_district=203 if knr==222 /* 222 nordre høland was part of 221 høland */
replace SMD_district=203 if knr==225
replace SMD_district=203 if knr==228
*replace SMD_district=XXX if knr==301  /* oslo */
replace SMD_district=406 if knr==435
*replace SMD_district=XXX if knr==602 /* drammen */
replace SMD_district=902 if knr==917
*replace SMD_district=XXX if knr==1001
*replace SMD_district=XXX if knr==1103
replace SMD_district=1103 if knr==1125
replace SMD_district=1103 if knr==1127
replace SMD_district=1103 if knr==1144
replace SMD_district=1104 if knr==1151
replace SMD_district=1105 if knr==1156
replace SMD_district=1105 if knr==1158
replace SMD_district=1204 if knr==1256
replace SMD_district=1204 if knr==1260
replace SMD_district=1204 if knr==1262
*replace SMD_district=XXX if knr==1301
replace SMD_district=1404 if knr==1435
replace SMD_district=1404 if knr==1436
replace SMD_district=1502 if knr==1518
replace SMD_district=1504 if knr==1547
*replace SMD_district= if knr==1601
replace SMD_district=1602 if knr==1611
replace SMD_district=1602 if knr==1613
replace SMD_district=1603 if knr==1637
replace SMD_district=1603 if knr==1639
replace SMD_district=1604 if knr==1641
replace SMD_district=1604 if knr==1642
replace SMD_district=1604 if knr==1643
*replace SMD_district= if knr==1701
replace SMD_district=1703 if knr==1739
replace SMD_district=1703 if knr==1740
replace SMD_district=1703 if knr==1741
replace SMD_district=1801 if knr==1801
replace SMD_district=1802 if knr==1803
replace SMD_district=1802 if knr==1823
replace SMD_district=1802 if knr==1825
replace SMD_district=1802 if knr==1829
replace SMD_district=1802 if knr==1831
replace SMD_district=1804 if knr==1854
replace SMD_district=1805 if knr==1856
replace SMD_district=1805 if knr==1861
replace SMD_district=1805 if knr==1863
replace SMD_district=1806 if knr==1869
replace SMD_district=1806 if knr==1871
replace SMD_district=1806 if knr==1873
replace SMD_district=1901 if knr==1912
replace SMD_district=1901 if knr==1913
replace SMD_district=1902 if knr==1916
replace SMD_district=1902 if knr==1918
replace SMD_district=1902 if knr==1919
replace SMD_district=1903 if knr==1923
replace SMD_district=1904 if knr==1939
replace SMD_district=1904 if knr==1940

replace SMD_district=. if SMD_district<100 & year>1920 /* we cannot get a unique SMD post-reform for big cities: oslo (5), drammen (2), kristiansand (2) stavanger (2), bergen (4), trondheim (4) */
replace PR_district=22 if knr==3001
replace PR_district=22 if knr==3002
replace PR_district=22 if knr==3003
replace PR_district=22 if knr==3004
replace PR_district=22 if knr==3005

replace PR_district=24 if knr==6001
replace PR_district=24 if knr==6002

replace PR_district=27 if knr==10001
replace PR_district=27 if knr==10002

replace PR_district=27 if knr==11001
replace PR_district=27 if knr==11002

replace PR_district=28 if knr==13001
replace PR_district=28 if knr==13002
replace PR_district=28 if knr==13003
replace PR_district=28 if knr==13004
replace PR_district=28 if knr==1280 /* aarstad , part of bergen from 1915 */

replace PR_district=30 if knr==16001
replace PR_district=30 if knr==16002  /*includes levanger*/
replace PR_district=30 if knr==16003
replace PR_district=30 if knr==16004

sum knr PR* SMD* if year==1918
sum knr PR* SMD* if year==1921  /* missing some SMDs since we cannot split up Oslo */

rename SMD_di SMD
rename PR_di PR
rename rep M
keep year knr PR SMD M votes1* votes2* 
order year knr PR SMD M votes1* votes2* 

save dta/MunicipalityLevel.dta, replace

******************************************************************
****************** CREATING SMD DISTRICT VARIABLES ***************
******************************************************************
*preserve

*** Pre-reform SMDs within municipalities, reduce number of districts from 126 to 107
drop if knr>3000  /* Cannot measure at the SMD-level post-reform */
drop if knr==301|knr==602|knr==1001|knr==1103|knr==1301|knr==1601|knr==1701 /* big cities post-reform*/

*** In the transition from SMD to PR a few districts are split, this reduce number of districts to 104
drop if SMD==892 /* Brevik and Holmestrand (SMD_district 892) was put in two different PR_districts (25 and 26). */
drop if SMD==1001 /* Mandal (SMD_district 1001, rural) was moved to an urban PR_district (27), while the other SMD_districts in 1001 went to PR district 10 (together with 1002, 1003 and 1004). */
drop if SMD==690  /* Kongsberg, Hønefoss og Notodden (SMD_district 690) was put in two different PR_districts (24 and 25)*/

** From 1915 to 1918 the structure change same somewhat: 
drop if SMD==500|SMD==490 /* Lillehammer, Hamar, Gjøvik and Kongsvinger split in two from 1915 to 1918 */
/* o	Extra district added in Romsdal, and then a few municipalities are moved around*/
drop if SMD==1501
drop if SMD==1502
drop if SMD==1503
drop if SMD==1504
drop if SMD==1505
drop if SMD==1506
/* o	Extra district added in Troms, and then a few municipalities are moved around */
drop if SMD==1901
drop if SMD==1902
drop if SMD==1903
drop if SMD==1904
/* 1280 Aarstad leaves SMD 1203 and becomes part of Bergen from 1915, onwards */
drop if SMD==1203

collapse (mean) M (mean) PR (sum) votes1* (sum) votes2*, by(year SMD)

gen temp=1
egen count=sum(temp),by(year)
assert count==91  /* 91 districts -- the discrepancy from Cox-Fiva-Smith is that Aarstad 1280 is excluded */
drop count temp

foreach round in 1 2 {
	foreach parti in s v_a v a l t kir h_fv h fv oth nsa nkp {
	gen vs`round'_`parti'=votes`round'_`parti'/votes`round'_TOTAL
	gen run`round'_`parti'=0
	replace run`round'_`parti'=1 if votes`round'_`parti'!=0
	}
}

*************************************************************************************************************************************************
************** discovered small error september 2018, *******************************************************************************************
replace run1_h_fv = 1 if SMD == 1591 & year ==1924 /* H&FV received zero votes, but did run in kjøpsteder i Møre fylke */
replace run1_oth = 1 if SMD == 1590 & year ==1924 /* Kristiansund samlingsparti received zero votes, but did run in kjøpsteder i Møre fylke  */
*************************************************************************************************************************************************

order year SMD M votes1* vs1* run1* votes2* vs2* run2*
	
save dta/SMDLevel.dta, replace
*restore

******************************************************************
****************** CREATING PR DISTRICT VARIABLES ****************
******************************************************************
*egen M2=max(M), by(PR)
*replace M=M2 
*drop M2
*collapse (max) M (sum) votes_*, by(year PR)
*foreach parti in s v_a v a l t kir h_fv h fv oth nsa nkp {
*gen vs_`parti'=votes_`parti'/votes_total
*}

*save dta/PRLevel.dta, replace
